package com.yupi.springbootinit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.mapper.ReportMapper;
import com.yupi.springbootinit.model.dto.report.ReportAddRequest;
import com.yupi.springbootinit.model.entity.Report;
import com.yupi.springbootinit.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {


    @Resource
    private ReportService reportService;
    @Resource
    private ReportMapper reportMapper;
    /**
     * 举报
     */

    @PostMapping("/addreport")
    public BaseResponse addReport(@RequestBody ReportAddRequest report, HttpServletRequest httpServletRequest) {

        if (report == null) {
            ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        Report report1 = new Report();
        report1.setPostId(report.getPostId());
        report1.setCommentId(report.getCommentId());
        report1.setCommentrqId(report.getCommentrqId());
        report1.setUserId(report.getUserId());
        reportService.save(report1);

        return ResultUtils.success(null);
    }

    /**
     * 获取举报列表(管理员权限)
     * @return
     */
    @GetMapping("/getreport")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<Report>> getReport() {
        List<Report> list = reportService.list();
        return ResultUtils.success(list);

    }

    @GetMapping("/deletereport")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<String> deleteReport(@RequestParam("reportId") Long reportId){
        boolean result = reportService.removeById(reportId);
        if (!result) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success("删除成功");
    }
}
